//
//  DRLoginDataModel.m
//  DoctorRen
//
//  Created by Lemon on 2018/11/20.
//

#import "DRLoginDataModel.h"
#import <YYModel/YYModel.h>
#import <UMAnalytics/MobClick.h>

@implementation DRLoginDataModel

+(DRUserModel *)getUserModel{
    NSString *userJson = [[NSUserDefaults standardUserDefaults] objectForKey:@"kUserModel"];
    return [DRUserModel yy_modelWithJSON:userJson];
}

+(NSString *)getUserToken{
    NSString *token = [self getUserModel].token;
    return token ? : @"";
}

+(void)saveUserModel:(DRUserModel *)model{
    NSString *userJson = [model yy_modelToJSONString];
    [[NSUserDefaults standardUserDefaults] setObject:userJson forKey:@"kUserModel"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(BOOL)isLogin{
    BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"kUserIsLogin"];
    return isLogin;
}

+(void)setIsLogin:(BOOL)isLogin{
    [[NSUserDefaults standardUserDefaults] setBool:isLogin forKey:@"kUserIsLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(void)loginWithPhone:(NSString *)phone Password:(NSString *)password Success:(void (^)(DRUserModel * _Nonnull))success Fail:(RequestFail)fail{
    
    [[DRHttpTool shared] POST:DRURL(@"index.php/apisimple/login_act.html") andParams:@{@"phone":phone, @"password" : password} andSucc:^(id data) {
        DRUserModel *model = [DRUserModel yy_modelWithJSON:data];
        model.realPw = password;
        [DRLoginDataModel saveUserModel:model];
        [MobClick profileSignInWithPUID:model.token];
        if (success) {
            success(model);
        }
    } andFail:^(NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
    }];
}

+(void)registerWithPhone:(NSString *)phone Password:(NSString *)password MsgCode:(NSString *)code Success:(void (^)(void))success Fail:(RequestFail)fail{
    
    [[DRHttpTool shared] POST:DRURL(@"index.php/apisimple/reg_act.html") andParams:@{@"phone":phone, @"password" : password,@"code": code} andSucc:^(id  _Nonnull data) {
        if (success) {
            DRUserModel *model = [[DRUserModel alloc] init];
            model.phone = phone;
            model.realPw = password;
            [DRLoginDataModel saveUserModel:model];
            success();
        }
    } andFail:^(NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
    }];
    
}

+(void)forgetPasswordWithPhone:(NSString *)phone Password:(NSString *)password MsgCode:(NSString *)code Success:(void (^)(void))success Fail:(RequestFail)fail{
    
    [[DRHttpTool shared] POST:DRURL(@"index.php/apisimple/forget_act.html") andParams:@{@"phone":phone, @"password" : password, @"code": code} andSucc:^(id  _Nonnull data) {
        if (success) {
            DRUserModel *model = [[DRUserModel alloc] init];
            model.phone = phone;
            model.realPw = password;
            [DRLoginDataModel saveUserModel:model];
            success();
        }
    } andFail:^(NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
    }];
    
}

+(void)sendMsgCodeWithPhone:(NSString *)phone Success:(void (^)(void))success Fail:(RequestFail)fail{
    
    [[DRHttpTool shared] POST:DRURL(@"index.php/Sample/send_sms.html") andParams:@{@"phone" : phone} andSucc:^(id  _Nonnull data) {
        if (success) {
            success();
        }
    } andFail:^(NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
    }];
    
}

+(void) thirdPartyLoginWithType:(NSInteger) type unionid:(NSString *)unionid Success:(void (^)(DRUserModel * user))success Fail:(RequestFail)fail{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (type == 1) {
        //微信登录
        params[@"type"] = @"1";
        params[@"unionid"] = unionid;
    }else{
        params[@"openid"] = unionid;
        params[@"type"] = @"2";
    }
    [[DRHttpTool shared] POST:DRURL(@"index.php/apisimple/threewaylogin.html") andParams:params andSucc:^(id  _Nonnull data) {
        if (success) {
            DRUserModel *model = [DRUserModel yy_modelWithJSON:data];
            [DRLoginDataModel saveUserModel:model];
            [MobClick profileSignInWithPUID:model.token];
            success(model);
        }
    } andFail:^(NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
    }];
}

+(void)bindPhoneWithPhone:(NSString *)phone Type:(NSInteger)type Uid:(NSString *)uid Password:(NSString *)password Code:(NSString *)code Success:(void (^)(DRUserModel * _Nonnull))success Fail:(RequestFail)fail{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (type == 1) {
        //微信登录
        params[@"type"] = @"1";
        params[@"unionid"] = uid;
    }else{
        params[@"openid"] = uid;
        params[@"type"] = @"2";
    }
    params[@"phone"] = phone;
    params[@"password"] = password;
    params[@"code"] = code;
    
    [[DRHttpTool shared] POST:DRURL(@"index.php/apisimple/bindphone.html") andParams:params andSucc:^(id  _Nonnull data) {
        if (success) {
            DRUserModel *model = [DRUserModel yy_modelWithJSON:data];
            [DRLoginDataModel saveUserModel:model];
            [MobClick profileSignInWithPUID:model.token];
            success(model);
        }
    } andFail:^(NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
    }];
    
}

@end
